/*
 * @Author: szx
 * @Date: 2022-06-07 14:32:06
 * @LastEditTime: 2022-06-07 15:51:44
 * @Description:
 * @FilePath: \leetcode\面试题\16\10\10.js
 */
/**
 * @param {number[]} birth
 * @param {number[]} death
 * @return {number}
 */
var maxAliveYear = function (birth, death) {
    const map = new Map();
    for (let i of birth) {
        map.set(i, (map.get(i) || 0) + 1);
    }
    for (let i = 0; i < birth.length; i++) {
        for (let m of map) {
            if (birth[i] < m[0] && m[0] <= death[i]) map.set(m[0], m[1] + 1);
        }
    }
    let res = 0,
        max = 0;
    for (let m of map) {
        if (m[1] > max) {
            max = m[1];
            res = m[0];
        }
    }
    return res;
};

var maxAliveYear = function (birth, death) {
    birth.sort((a, b) => a - b);
    const map = new Map();
    for (let i of birth) {
        map.set(i, (map.get(i) || 0) + 1);
    }
    for (let i = 0; i < birth.length; i++) {
        for (let m of map) {
            if (birth[i] < m[0] && m[0] <= death[i]) map.set(m[0], m[1] + 1);
        }
    }
    let res = 0,
        max = 0;
    for (let m of map) {
        if (m[1] > max) {
            max = m[1];
            res = m[0];
        }
    }
    return res;
};

var maxAliveYear = function (birth, death) {
    const change = new Array(102).fill(0);
    for (let i = 0; i < birth.length; i++) {
        change[birth[i] - 1900]++;
        change[death[i] - 1899]--;
    }
    let maxAlive = 0,
        curAlive = 0,
        res = 1900;
    for (let i = 0; i < 101; i++) {
        curAlive += change[i];
        if (curAlive > maxAlive) {
            maxAlive = curAlive;
            res = 1900 + i;
        }
    }
    return res;
};
